home *** CD-ROM | disk | FTP | other *** search
/ Sprite 1984 - 1993 / Sprite 1984 - 1993.iso / src / cmds / gdb.new / sprite / gdb / coffread.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-08-31  |  53.9 KB  |  2,044 lines

  1. /* Read coff symbol tables and convert to internal format, for GDB.
  2.    Design and support routines derived from dbxread.c, and UMAX COFF
  3.    specific routines written 9/1/87 by David D. Johnson, Brown University.
  4.    Revised 11/27/87 ddj@cs.brown.edu
  5.    Copyright (C) 1987-1991 Free Software Foundation, Inc.
  6.  
  7. This file is part of GDB.
  8.  
  9. This program is free software; you can redistribute it and/or modify
  10. it under the terms of the GNU General Public License as published by
  11. the Free Software Foundation; either version 2 of the License, or
  12. (at your option) any later version.
  13.  
  14. This program is distributed in the hope that it will be useful,
  15. but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  17. GNU General Public License for more details.
  18.  
  19. You should have received a copy of the GNU General Public License
  20. along with this program; if not, write to the Free Software
  21. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  */
  22.  
  23. #include <stdio.h>
  24. #include "defs.h"
  25. #include "param.h"
  26. #include "symtab.h"
  27. #include "breakpoint.h"
  28. #include "bfd.h"
  29. #include "symfile.h"
  30.  
  31. #if defined (TDESC)
  32. /* Need to get C_VERSION and friends.  */
  33. #include <a.out.h>
  34. #else /* not TDESC */
  35. #include <intel-coff.h>
  36. #endif /* not TDESC */
  37.  
  38. #include <obstack.h>
  39. #include <string.h>
  40.  
  41. #include "internalcoff.h"    /* Internal format of COFF symbols in BFD */
  42. #include "libcoff.h"        /* FIXME secret internal data from BFD */
  43.  
  44. static void add_symbol_to_list ();
  45. static void read_coff_symtab ();
  46. static void patch_opaque_types ();
  47. static struct type *decode_function_type ();
  48. static struct type *decode_type ();
  49. static struct type *decode_base_type ();
  50. static struct type *read_enum_type ();
  51. static struct type *read_struct_type ();
  52. static void finish_block ();
  53. static struct blockvector *make_blockvector ();
  54. static struct symbol *process_coff_symbol ();
  55. static int init_stringtab ();
  56. static void free_stringtab ();
  57. static char *getfilename ();
  58. static char *getsymname ();
  59. static int init_lineno ();
  60. static void enter_linenos ();
  61. static void read_one_sym ();
  62.  
  63. extern int fclose ();
  64. extern void free_all_symtabs ();
  65. extern void free_all_psymtabs ();
  66.  
  67. /* To be an sdb debug type, type must have at least a basic or primary
  68.    derived type.  Using this rather than checking against T_NULL is
  69.    said to prevent core dumps if we try to operate on Michael Bloom
  70.    dbx-in-coff file.  */
  71.  
  72. #define SDB_TYPE(type) (BTYPE(type) | (type & N_TMASK))
  73.  
  74. /* Name of source file whose symbol data we are now processing.
  75.    This comes from a symbol named ".file".  */
  76.  
  77. static char *last_source_file;
  78.  
  79. /* Core address of start and end of text of current source file.
  80.    This comes from a ".text" symbol where x_nlinno > 0.  */
  81.  
  82. static CORE_ADDR cur_src_start_addr;
  83. static CORE_ADDR cur_src_end_addr;
  84.  
  85. /* Core address of the end of the first object file.  */
  86. static CORE_ADDR first_object_file_end;
  87.  
  88. /* End of the text segment of the executable file,
  89.    as found in the symbol _etext.  */
  90.  
  91. static CORE_ADDR end_of_text_addr;
  92.  
  93. /* The addresses of the symbol table stream and number of symbols
  94.    of the object file we are reading (as copied into core).  */
  95.  
  96. static FILE *nlist_stream_global;
  97. static int nlist_nsyms_global;
  98.  
  99. /* The entry point (starting address) of the file, if it is an executable.  */
  100.  
  101. static CORE_ADDR entry_point;
  102.  
  103. /* The index in the symbol table of the last coff symbol that was processed.  */
  104.  
  105. static int symnum;
  106.  
  107. /* Vector of types defined so far, indexed by their coff symnum.  */
  108.  
  109. static struct typevector *type_vector;
  110.  
  111. /* Number of elements allocated for type_vector currently.  */
  112.  
  113. static int type_vector_length;
  114.  
  115. /* Vector of line number information.  */
  116.  
  117. static struct linetable *line_vector;
  118.  
  119. /* Index of next entry to go in line_vector_index.  */
  120.  
  121. static int line_vector_index;
  122.  
  123. /* Last line number recorded in the line vector.  */
  124.  
  125. static int prev_line_number;
  126.  
  127. /* Number of elements allocated for line_vector currently.  */
  128.  
  129. static int line_vector_length;
  130.  
  131. #ifdef TDESC
  132. #include "tdesc.h"
  133. #define SEM
  134. int int_sem_val = 's' << 24 | 'e' << 16 | 'm' << 8 | '.';
  135. int temp_sem_val;
  136. int last_coffsem = 2;
  137. #if 0
  138.   /* This isn't used currently.  */
  139. int last_coffsyn = 0;
  140. #endif
  141. int debug_info = 0;    /*used by tdesc */
  142. extern dc_dcontext_t tdesc_handle;
  143. extern int safe_to_init_tdesc_context;
  144. #endif
  145.  
  146. /* Chain of typedefs of pointers to empty struct/union types.
  147.    They are chained thru the SYMBOL_VALUE_CHAIN.  */
  148.  
  149. #define HASHSIZE 127
  150. static struct symbol *opaque_type_chain[HASHSIZE];
  151.  
  152. /* Record the symbols defined for each context in a list.
  153.    We don't create a struct block for the context until we
  154.    know how long to make it.  */
  155.  
  156. struct pending
  157. {
  158.   struct pending *next;
  159.   struct symbol *symbol;
  160. };
  161.  
  162. /* Here are the three lists that symbols are put on.  */
  163.  
  164. struct pending *file_symbols;    /* static at top level, and types */
  165.  
  166. struct pending *global_symbols;    /* global functions and variables */
  167.  
  168. struct pending *local_symbols;    /* everything local to lexical context */
  169.  
  170. /* List of unclosed lexical contexts
  171.    (that will become blocks, eventually).  */
  172.  
  173. struct context_stack
  174. {
  175.   struct context_stack *next;
  176.   struct pending *locals;
  177.   struct pending_block *old_blocks;
  178.   struct symbol *name;
  179.   CORE_ADDR start_addr;
  180.   int depth;
  181. };
  182.  
  183. struct context_stack *context_stack;
  184.  
  185. /* Nonzero if within a function (so symbols should be local,
  186.    if nothing says specifically).  */
  187.  
  188. int within_function;
  189.  
  190. /* List of blocks already made (lexical contexts already closed).
  191.    This is used at the end to make the blockvector.  */
  192.  
  193. struct pending_block
  194. {
  195.   struct pending_block *next;
  196.   struct block *block;
  197. };
  198.  
  199. struct pending_block *pending_blocks;
  200.  
  201. extern CORE_ADDR startup_file_start;    /* From blockframe.c */
  202. extern CORE_ADDR startup_file_end;    /* From blockframe.c */
  203.  
  204. /* Complaints about various problems in the file being read  */
  205.  
  206. struct complaint ef_complaint = 
  207.   {"Unmatched .ef symbol(s) ignored starting at symnum %d", 0, 0};
  208.  
  209. struct complaint lineno_complaint =
  210.   {"Line number pointer %d lower than start of line numbers", 0, 0};
  211.  
  212.  
  213. /* Look up a coff type-number index.  Return the address of the slot
  214.    where the type for that index is stored.
  215.    The type-number is in INDEX. 
  216.  
  217.    This can be used for finding the type associated with that index
  218.    or for associating a new type with the index.  */
  219.  
  220. static struct type **
  221. coff_lookup_type (index)
  222.      register int index;
  223. {
  224.   if (index >= type_vector_length)
  225.     {
  226.       int old_vector_length = type_vector_length;
  227.  
  228.       type_vector_length *= 2;
  229.       if (type_vector_length < index) {
  230.     type_vector_length = index * 2;
  231.       }
  232.       type_vector = (struct typevector *)
  233.     xrealloc (type_vector, sizeof (struct typevector)
  234.                 + type_vector_length * sizeof (struct type *));
  235.       bzero (&type_vector->type[ old_vector_length ],
  236.          (type_vector_length - old_vector_length) * sizeof(struct type *));
  237.     }
  238.   return &type_vector->type[index];
  239. }
  240.  
  241. /* Make sure there is a type allocated for type number index
  242.    and return the type object.
  243.    This can create an empty (zeroed) type object.  */
  244.  
  245. static struct type *
  246. coff_alloc_type (index)
  247.      int index;
  248. {
  249.   register struct type **type_addr = coff_lookup_type (index);
  250.   register struct type *type = *type_addr;
  251.  
  252.   /* If we are referring to a type not known at all yet,
  253.      allocate an empty type for it.
  254.      We will fill it in later if we find out how.  */
  255.   if (type == 0)
  256.     {
  257.       type = (struct type *) obstack_alloc (symbol_obstack,
  258.                         sizeof (struct type));
  259.       bzero (type, sizeof (struct type));
  260.       *type_addr = type;
  261.     }
  262.   return type;
  263. }
  264.  
  265. /* maintain the lists of symbols and blocks */
  266.  
  267. /* Add a symbol to one of the lists of symbols.  */
  268. static void
  269. add_symbol_to_list (symbol, listhead)
  270.      struct symbol *symbol;
  271.      struct pending **listhead;
  272. {
  273.   register struct pending *link
  274.     = (struct pending *) xmalloc (sizeof (struct pending));
  275.  
  276.   link->next = *listhead;
  277.   link->symbol = symbol;
  278.   *listhead = link;
  279. }
  280.  
  281. /* Take one of the lists of symbols and make a block from it.
  282.    Put the block on the list of pending blocks.  */
  283.  
  284. static void
  285. finish_block (symbol, listhead, old_blocks, start, end)
  286.      struct symbol *symbol;
  287.      struct pending **listhead;
  288.      struct pending_block *old_blocks;
  289.      CORE_ADDR start, end;
  290. {
  291.   register struct pending *next, *next1;
  292.   register struct block *block;
  293.   register struct pending_block *pblock;
  294.   struct pending_block *opblock;
  295.   register int i;
  296.  
  297.   /* Count the length of the list of symbols.  */
  298.  
  299.   for (next = *listhead, i = 0; next; next = next->next, i++);
  300.  
  301.   block = (struct block *)
  302.         obstack_alloc (symbol_obstack, sizeof (struct block) + (i - 1) * sizeof (struct symbol *));
  303.  
  304.   /* Copy the symbols into the block.  */
  305.  
  306.   BLOCK_NSYMS (block) = i;
  307.   for (next = *listhead; next; next = next->next)
  308.     BLOCK_SYM (block, --i) = next->symbol;
  309.  
  310.   BLOCK_START (block) = start;
  311.   BLOCK_END (block) = end;
  312.   BLOCK_SUPERBLOCK (block) = 0;    /* Filled in when containing block is made */
  313.  
  314.   /* Put the block in as the value of the symbol that names it.  */
  315.  
  316.   if (symbol)
  317.     {
  318.       SYMBOL_BLOCK_VALUE (symbol) = block;
  319.       BLOCK_FUNCTION (block) = symbol;
  320.     }
  321.   else
  322.     BLOCK_FUNCTION (block) = 0;
  323.  
  324.   /* Now free the links of the list, and empty the list.  */
  325.  
  326.   for (next = *listhead; next; next = next1)
  327.     {
  328.       next1 = next->next;
  329.       free (next);
  330.     }
  331.   *listhead = 0;
  332.  
  333.   /* Install this block as the superblock
  334.      of all blocks made since the start of this scope
  335.      that don't have superblocks yet.  */
  336.  
  337.   opblock = 0;
  338.   for (pblock = pending_blocks; pblock != old_blocks; pblock = pblock->next)
  339.     {
  340.       if (BLOCK_SUPERBLOCK (pblock->block) == 0)
  341.     BLOCK_SUPERBLOCK (pblock->block) = block;
  342.       opblock = pblock;
  343.     }
  344.  
  345.   /* Record this block on the list of all blocks in the file.
  346.      Put it after opblock, or at the beginning if opblock is 0.
  347.      This puts the block in the list after all its subblocks.  */
  348.  
  349.   pblock = (struct pending_block *) xmalloc (sizeof (struct pending_block));
  350.   pblock->block = block;
  351.   if (opblock)
  352.     {
  353.       pblock->next = opblock->next;
  354.       opblock->next = pblock;
  355.     }
  356.   else
  357.     {
  358.       pblock->next = pending_blocks;
  359.       pending_blocks = pblock;
  360.     }
  361. }
  362.  
  363. static struct blockvector *
  364. make_blockvector ()
  365. {
  366.   register struct pending_block *next, *next1;
  367.   register struct blockvector *blockvector;
  368.   register int i;
  369.  
  370.   /* Count the length of the list of blocks.  */
  371.  
  372.   for (next = pending_blocks, i = 0; next; next = next->next, i++);
  373.  
  374.   blockvector = (struct blockvector *)
  375.           obstack_alloc (symbol_obstack, sizeof (struct blockvector) + (i - 1) * sizeof (struct block *));
  376.  
  377.   /* Copy the blocks into the blockvector.
  378.      This is done in reverse order, which happens to put
  379.      the blocks into the proper order (ascending starting address).
  380.      finish_block has hair to insert each block into the list
  381.      after its subblocks in order to make sure this is true.  */
  382.  
  383.   BLOCKVECTOR_NBLOCKS (blockvector) = i;
  384.   for (next = pending_blocks; next; next = next->next)
  385.     BLOCKVECTOR_BLOCK (blockvector, --i) = next->block;
  386.  
  387.   /* Now free the links of the list, and empty the list.  */
  388.  
  389.   for (next = pending_blocks; next; next = next1)
  390.     {
  391.       next1 = next->next;
  392.       free (next);
  393.     }
  394.   pending_blocks = 0;
  395.  
  396.   return blockvector;
  397. }
  398.  
  399. /* Manage the vector of line numbers.  */
  400.  
  401. static void
  402. record_line (line, pc)
  403.      int line;
  404.      CORE_ADDR pc;
  405. {
  406.   struct linetable_entry *e;
  407.   /* Make sure line vector is big enough.  */
  408.  
  409.   if (line_vector_index + 2 >= line_vector_length)
  410.     {
  411.       line_vector_length *= 2;
  412.       line_vector = (struct linetable *)
  413.     xrealloc (line_vector, sizeof (struct linetable)
  414.           + (line_vector_length
  415.              * sizeof (struct linetable_entry)));
  416.     }
  417.  
  418.   e = line_vector->item + line_vector_index++;
  419.   e->line = line; e->pc = pc;
  420. }
  421.  
  422. /* Start a new symtab for a new source file.
  423.    This is called when a COFF ".file" symbol is seen;
  424.    it indicates the start of data for one original source file.  */
  425.  
  426. static void
  427. start_symtab ()
  428. {
  429.   file_symbols = 0;
  430.   global_symbols = 0;
  431.   context_stack = 0;
  432.   within_function = 0;
  433.   last_source_file = 0;
  434. #ifdef TDESC
  435.   last_coffsem = 2;
  436. #if 0
  437.   /* This isn't used currently.  */
  438.   last_coffsyn = 0;
  439. #endif
  440. #endif
  441.  
  442.   /* Initialize the source file information for this file.  */
  443.  
  444.   line_vector_index = 0;
  445.   line_vector_length = 1000;
  446.   prev_line_number = -2;    /* Force first line number to be explicit */
  447.   line_vector = (struct linetable *)
  448.     xmalloc (sizeof (struct linetable)
  449.          + line_vector_length * sizeof (struct linetable_entry));
  450. }
  451.  
  452. /* Save the vital information from when starting to read a file,
  453.    for use when closing off the current file.
  454.    NAME is the file name the symbols came from, START_ADDR is the first
  455.    text address for the file, and SIZE is the number of bytes of text.  */
  456.  
  457. static void
  458. complete_symtab (name, start_addr, size)
  459.     char *name;
  460.     CORE_ADDR start_addr;
  461.     unsigned int size;
  462. {
  463.   last_source_file = savestring (name, strlen (name));
  464.   cur_src_start_addr = start_addr;
  465.   cur_src_end_addr = start_addr + size;
  466.  
  467.   if (entry_point < cur_src_end_addr
  468.       && entry_point >= cur_src_start_addr)
  469.     {
  470.       startup_file_start = cur_src_start_addr;
  471.       startup_file_end = cur_src_end_addr;
  472.     }
  473. }
  474.  
  475. /* Finish the symbol definitions for one main source file,
  476.    close off all the lexical contexts for that file
  477.    (creating struct block's for them), then make the
  478.    struct symtab for that file and put it in the list of all such. */
  479.  
  480. static void
  481. end_symtab ()
  482. {
  483.   register struct symtab *symtab;
  484.   register struct context_stack *cstk;
  485.   register struct blockvector *blockvector;
  486.   register struct linetable *lv;
  487.  
  488.   /* Finish the lexical context of the last function in the file.  */
  489.  
  490.   if (context_stack)
  491.     {
  492.       cstk = context_stack;
  493.       context_stack = 0;
  494.       /* Make a block for the local symbols within.  */
  495.       finish_block (cstk->name, &local_symbols, cstk->old_blocks,
  496.             cstk->start_addr, cur_src_end_addr);
  497.       free (cstk);
  498.     }
  499.  
  500.   /* Ignore a file that has no functions with real debugging info.  */
  501.   if (pending_blocks == 0 && file_symbols == 0 && global_symbols == 0)
  502.     {
  503.       free (line_vector);
  504.       line_vector = 0;
  505.       line_vector_length = -1;
  506.       last_source_file = 0;
  507.       return;
  508.     }
  509.  
  510.   /* Create the two top-level blocks for this file (STATIC_BLOCK and
  511.      GLOBAL_BLOCK).  */
  512.   finish_block (0, &file_symbols, 0, cur_src_start_addr, cur_src_end_addr);
  513.   finish_block (0, &global_symbols, 0, cur_src_start_addr, cur_src_end_addr);
  514.  
  515.   /* Create the blockvector that points to all the file's blocks.  */
  516.   blockvector = make_blockvector ();
  517.  
  518.   /* Now create the symtab object for this source file.  */
  519.   symtab = (struct symtab *) xmalloc (sizeof (struct symtab));
  520.   symtab->free_ptr = 0;
  521.  
  522.   /* Fill in its components.  */
  523.   symtab->blockvector = blockvector;
  524.   symtab->free_code = free_linetable;
  525.   symtab->filename = last_source_file;
  526.   symtab->dirname = NULL;
  527.   lv = line_vector;
  528.   lv->nitems = line_vector_index;
  529.   symtab->linetable = (struct linetable *)
  530.     xrealloc (lv, (sizeof (struct linetable)
  531.            + lv->nitems * sizeof (struct linetable_entry)));
  532.   symtab->nlines = 0;
  533.   symtab->line_charpos = 0;
  534.  
  535.   symtab->language = language_unknown;
  536.   symtab->fullname = NULL;
  537.  
  538. #ifdef TDESC
  539.   symtab->coffsem = last_coffsem;
  540. #if 0
  541.   /* This isn't used currently.  Besides, if this is really about "syntax",
  542.      it shouldn't need to stick around past symbol read-in time.  */
  543.   symtab->coffsyn = last_coffsyn;
  544. #endif
  545. #endif
  546.  
  547.   free_named_symtabs (symtab->filename);
  548.  
  549.   /* Link the new symtab into the list of such.  */
  550.   symtab->next = symtab_list;
  551.   symtab_list = symtab;
  552.  
  553.   /* Reinitialize for beginning of new file. */
  554.   line_vector = 0;
  555.   line_vector_length = -1;
  556.   last_source_file = 0;
  557. }
  558.  
  559. static void
  560. record_misc_function (name, address)
  561.      char *name;
  562.      CORE_ADDR address;
  563. {
  564. #ifdef TDESC
  565.   /* We don't want TDESC entry points on the misc_function_vector */
  566.   if (name[0] == '@') return;
  567. #endif
  568.   /* mf_text isn't true, but apparently COFF doesn't tell us what it really
  569.      is, so this guess is more useful than mf_unknown.  */
  570.   prim_record_misc_function (savestring (name, strlen (name)),
  571.                  address,
  572.                  (int)mf_text);
  573. }
  574.  
  575. /* coff_symfile_init ()
  576.    is the coff-specific initialization routine for reading symbols.
  577.    It is passed a struct sym_fns which contains, among other things,
  578.    the BFD for the file whose symbols are being read, and a slot for
  579.    a pointer to "private data" which we fill with cookies and other
  580.    treats for coff_symfile_read ().
  581.  
  582.    We will only be called if this is a COFF or COFF-like file.
  583.    BFD handles figuring out the format of the file, and code in symtab.c
  584.    uses BFD's determination to vector to us.
  585.  
  586.    The ultimate result is a new symtab (or, FIXME, eventually a psymtab).  */
  587.  
  588. struct coff_symfile_info {
  589.   file_ptr min_lineno_offset;        /* Where in file lowest line#s are */
  590.   file_ptr max_lineno_offset;        /* 1+last byte of line#s in file */
  591. };
  592.  
  593. void
  594. coff_symfile_init (sf)
  595.      struct sym_fns *sf;
  596. {
  597.   bfd *abfd = sf->sym_bfd;
  598.  
  599.   /* Allocate struct to keep track of the symfile */
  600.   /* FIXME memory leak */
  601.   sf->sym_private = xmalloc (sizeof (struct coff_symfile_info));
  602.  
  603. #if defined (TDESC)
  604.   safe_to_init_tdesc_context  = 0;
  605. #endif
  606.  
  607.   /* Save startup file's range of PC addresses to help blockframe.c
  608.      decide where the bottom of the stack is.  */
  609.   if (bfd_get_file_flags (abfd) & EXEC_P)
  610.     {
  611.       /* Executable file -- record its entry point so we'll recognize
  612.      the startup file because it contains the entry point.  */
  613.       entry_point = bfd_get_start_address (abfd);
  614.     }
  615.   else
  616.     {
  617.       /* Examination of non-executable.o files.  Short-circuit this stuff.  */
  618.       /* ~0 will not be in any file, we hope.  */
  619.       entry_point = ~0;
  620.       /* set the startup file to be an empty range.  */
  621.       startup_file_start = 0;
  622.       startup_file_end = 0;
  623.     }
  624. }
  625.  
  626. /* This function is called for every section; it finds the outer limits
  627.    of the line table (minimum and maximum file offset) so that the
  628.    mainline code can read the whole thing for efficiency.  */
  629.  
  630. /* ARGSUSED */
  631. static void
  632. find_linenos (abfd, asect, vpinfo)
  633.      bfd *abfd;
  634.      sec_ptr asect;
  635.      PTR vpinfo;
  636. {
  637.   struct coff_symfile_info *info;
  638.   int size, count;
  639.   file_ptr offset, maxoff;
  640.  
  641. /* WARNING WILL ROBINSON!  ACCESSING BFD-PRIVATE DATA HERE!  FIXME!  */
  642.   count = asect->lineno_count;
  643. /* End of warning */
  644.  
  645.   if (count == 0)
  646.     return;
  647. #if !defined (LINESZ)
  648. /* Just in case, you never know what to expect from those
  649.    COFF header files.  */
  650. #define LINESZ (sizeof (struct lineno))
  651. #endif /* No LINESZ.  */
  652.   size = count * LINESZ;
  653.  
  654.   info = (struct coff_symfile_info *)vpinfo;
  655. /* WARNING WILL ROBINSON!  ACCESSING BFD-PRIVATE DATA HERE!  FIXME!  */
  656.   offset = asect->line_filepos;
  657. /* End of warning */
  658.  
  659.   if (offset < info->min_lineno_offset || info->min_lineno_offset == 0)
  660.     info->min_lineno_offset = offset;
  661.  
  662.   maxoff = offset + size;
  663.   if (maxoff > info->max_lineno_offset)
  664.     info->max_lineno_offset = maxoff;
  665. #ifdef TDESC
  666.   /* While we're at it, find the debug_info.  It's in the s_relptr
  667.      (or, in BFD-speak, rel_filepos) of the text segment section header.  */
  668.   if (strcmp (bfd_section_name (abfd, asect), ".text") == 0)
  669.     {
  670.       /* WARNING WILL ROBINSON!  ACCESSING BFD-PRIVATE DATA HERE!  FIXME!  */
  671.       debug_info = asect->rel_filepos;
  672.       /* End of warning */
  673.       if (tdesc_handle)
  674.     {
  675.       dc_terminate (tdesc_handle);
  676.       tdesc_handle = 0;
  677.     }
  678.     }
  679. #endif /* TDESC */
  680. }
  681.  
  682.  
  683. /* The BFD for this file -- only good while we're actively reading
  684.    symbols into a psymtab or a symtab.  */
  685.  
  686. static bfd *symfile_bfd;
  687.  
  688. /* Read a symbol file, after initialization by coff_symfile_init.  */
  689. /* FIXME!  Addr and Mainline are not used yet -- this will not work for
  690.    shared libraries or add_file!  */
  691.  
  692. /* ARGSUSED */
  693. void
  694. coff_symfile_read (sf, addr, mainline)
  695.      struct sym_fns *sf;
  696.      CORE_ADDR addr;
  697.      int mainline;
  698. {
  699.   struct coff_symfile_info *info = (struct coff_symfile_info *)sf->sym_private;
  700.   bfd *abfd = sf->sym_bfd;
  701.   char *name = bfd_get_filename (abfd);
  702.   int desc;
  703.   register int val;
  704.   int num_symbols;
  705.   int symtab_offset;
  706.   int stringtab_offset;
  707.  
  708.   symfile_bfd = abfd;            /* Kludge for swap routines */
  709.  
  710. /* WARNING WILL ROBINSON!  ACCESSING BFD-PRIVATE DATA HERE!  FIXME!  */
  711.    desc = fileno ((FILE *)(abfd->iostream));        /* File descriptor */
  712.    num_symbols = bfd_get_symcount (abfd);    /* How many syms */
  713.    symtab_offset = obj_sym_filepos (abfd);    /* Symbol table file offset */
  714.    stringtab_offset = symtab_offset + num_symbols * SYMESZ;  /* String tab */
  715. /* End of warning */
  716.  
  717.   /* Read the line number table, all at once.  */
  718.   info->min_lineno_offset = 0;
  719.   info->max_lineno_offset = 0;
  720.   bfd_map_over_sections (abfd, find_linenos, info);
  721.  
  722.   val = init_lineno (desc, info->min_lineno_offset, 
  723.              info->max_lineno_offset - info->min_lineno_offset);
  724.   if (val < 0)
  725.     error ("\"%s\": error reading line numbers\n", name);
  726.  
  727.   /* Now read the string table, all at once.  */
  728.  
  729.   val = init_stringtab (desc, stringtab_offset);
  730.   if (val < 0)
  731.     {
  732.       free_all_symtabs ();        /* FIXME blows whole symtab */
  733.       printf ("\"%s\": can't get string table", name);
  734.       fflush (stdout);
  735.       return;
  736.     }
  737.   make_cleanup (free_stringtab, 0);
  738.  
  739.   /* Position to read the symbol table.  Do not read it all at once. */
  740.   val = lseek (desc, (long)symtab_offset, 0);
  741.   if (val < 0)
  742.     perror_with_name (name);
  743.  
  744.   init_misc_bunches ();
  745.   make_cleanup (discard_misc_bunches, 0);
  746.  
  747.   /* Now that the executable file is positioned at symbol table,
  748.      process it and define symbols accordingly.  */
  749.  
  750.   read_coff_symtab (desc, num_symbols);
  751.  
  752.   patch_opaque_types ();
  753.  
  754.   /* Sort symbols alphabetically within each block.  */
  755.  
  756.   sort_all_symtab_syms ();
  757.  
  758.   /* Go over the misc symbol bunches and install them in vector.  */
  759.  
  760.   condense_misc_bunches (!mainline);
  761.  
  762.   /* Make a default for file to list.  */
  763.  
  764.   select_source_symtab (0);    /* FIXME, this might be too slow, see dbxread */
  765. }
  766.  
  767. void
  768. coff_new_init ()
  769. {
  770.   /* There seems to be nothing to do except free_all_symtabs and set
  771.      symfile to zero, which is done by our caller.  */
  772. }
  773.  
  774. /* Simplified internal version of coff symbol table information */
  775.  
  776. struct coff_symbol {
  777.   char *c_name;
  778.   int c_symnum;        /* symbol number of this entry */
  779.   int c_nsyms;        /* 1 if syment only, 2 if syment + auxent, etc */
  780.   long c_value;
  781.   int c_sclass;
  782.   int c_secnum;
  783.   unsigned int c_type;
  784. };
  785.  
  786. /* Given pointers to a symbol table in coff style exec file,
  787.    analyze them and create struct symtab's describing the symbols.
  788.    NSYMS is the number of symbols in the symbol table.
  789.    We read them one at a time using read_one_sym ().  */
  790.  
  791. static void
  792. read_coff_symtab (desc, nsyms)
  793.      int desc;
  794.      int nsyms;
  795. {
  796.   int newfd;            /* Avoid multiple closes on same desc */
  797.   FILE *stream; 
  798.   register struct context_stack *new;
  799.   struct coff_symbol coff_symbol;
  800.   register struct coff_symbol *cs = &coff_symbol;
  801.   static struct internal_syment main_sym;
  802.   static union internal_auxent main_aux;
  803.   struct coff_symbol fcn_cs_saved;
  804.   static struct internal_syment fcn_sym_saved;
  805.   static union internal_auxent fcn_aux_saved;
  806.  
  807.   /* A .file is open.  */
  808.   int in_source_file = 0;
  809.   int num_object_files = 0;
  810.   int next_file_symnum = -1;
  811.  
  812.   /* Name of the current file.  */
  813.   char *filestring = "";
  814.   int depth;
  815.   int fcn_first_line;
  816.   int fcn_last_line;
  817.   int fcn_start_addr;
  818.   long fcn_line_ptr;
  819.   struct cleanup *old_chain;
  820.  
  821.  
  822.   newfd = dup (desc);
  823.   if (newfd == -1)
  824.     fatal ("Too many open files");
  825.   stream = fdopen (newfd, "r");
  826.  
  827.   old_chain = make_cleanup (free_all_symtabs, 0);
  828.   make_cleanup (fclose, stream);
  829.   nlist_stream_global = stream;
  830.   nlist_nsyms_global = nsyms;
  831.   last_source_file = 0;
  832.   bzero (opaque_type_chain, sizeof opaque_type_chain);
  833.  
  834.   type_vector_length = 160;
  835.   type_vector = (struct typevector *)
  836.         xmalloc (sizeof (struct typevector)
  837.                 + type_vector_length * sizeof (struct type *));
  838.   bzero (type_vector->type, type_vector_length * sizeof (struct type *));
  839.  
  840.   start_symtab ();
  841.  
  842.   symnum = 0;
  843.   while (symnum < nsyms)
  844.     {
  845.       QUIT;            /* Make this command interruptable.  */
  846.       read_one_sym (cs, &main_sym, &main_aux);
  847.  
  848. #ifdef SEM
  849.       temp_sem_val = cs->c_name[0] << 24 | cs->c_name[1] << 16 |
  850.                      cs->c_name[2] << 8 | cs->c_name[3];
  851.       if (int_sem_val == temp_sem_val)
  852.         last_coffsem = (int) strtol (cs->c_name+4, (char **) NULL, 10);
  853. #endif
  854.  
  855.       if (cs->c_symnum == next_file_symnum && cs->c_sclass != C_FILE)
  856.     {
  857.       if (last_source_file)
  858.         end_symtab ();
  859.  
  860.       start_symtab ();
  861.       complete_symtab ("_globals_", 0, first_object_file_end);
  862.       /* done with all files, everything from here on out is globals */
  863.     }
  864.  
  865.       /* Special case for file with type declarations only, no text.  */
  866.       if (!last_source_file && SDB_TYPE (cs->c_type)
  867.       && cs->c_secnum == N_DEBUG)
  868.     complete_symtab (filestring, 0, 0);
  869.  
  870.       /* Typedefs should not be treated as symbol definitions.  */
  871.       if (ISFCN (cs->c_type) && cs->c_sclass != C_TPDEF)
  872.     {
  873.       /* record as misc function.  if we get '.bf' next,
  874.        * then we undo this step
  875.        */
  876.       record_misc_function (cs->c_name, cs->c_value);
  877.  
  878.       fcn_line_ptr = main_aux.x_sym.x_fcnary.x_fcn.x_lnnoptr;
  879.       fcn_start_addr = cs->c_value;
  880.       fcn_cs_saved = *cs;
  881.       fcn_sym_saved = main_sym;
  882.       fcn_aux_saved = main_aux;
  883.       continue;
  884.     }
  885.  
  886.       switch (cs->c_sclass)
  887.     {
  888.       case C_EFCN:
  889.       case C_EXTDEF:
  890.       case C_ULABEL:
  891.       case C_USTATIC:
  892.       case C_LINE:
  893.       case C_ALIAS:
  894.       case C_HIDDEN:
  895.         printf ("Bad n_sclass = %d\n", cs->c_sclass);
  896.         break;
  897.  
  898.       case C_FILE:
  899.         /*
  900.          * c_value field contains symnum of next .file entry in table
  901.          * or symnum of first global after last .file.
  902.          */
  903.         next_file_symnum = cs->c_value;
  904.         filestring = getfilename (&main_aux);
  905.         /*
  906.          * Complete symbol table for last object file
  907.          * containing debugging information.
  908.          */
  909.         if (last_source_file)
  910.           {
  911.         end_symtab ();
  912.         start_symtab ();
  913.           }
  914.         in_source_file = 1;
  915.         break;
  916.  
  917.           case C_STAT:
  918.         if (cs->c_name[0] == '.') {
  919.             if (strcmp (cs->c_name, _TEXT) == 0) {
  920.                 if (++num_object_files == 1) {
  921.                     /* last address of startup file */
  922.                     first_object_file_end = cs->c_value +
  923.                         main_aux.x_scn.x_scnlen;
  924.                 }
  925.                 /* Check for in_source_file deals with case of
  926.                    a file with debugging symbols
  927.                    followed by a later file with no symbols.  */
  928.                 if (in_source_file)
  929.                   complete_symtab (filestring, cs->c_value,
  930.                            main_aux.x_scn.x_scnlen);
  931.                 in_source_file = 0;
  932.             }
  933.             /* flush rest of '.' symbols */
  934.             break;
  935.         }
  936.         else if (!SDB_TYPE (cs->c_type)
  937.              && cs->c_name[0] == 'L'
  938.              && (strncmp (cs->c_name, "LI%", 3) == 0
  939.              || strncmp (cs->c_name, "LF%", 3) == 0
  940.              || strncmp (cs->c_name,"LC%",3) == 0
  941.              || strncmp (cs->c_name,"LP%",3) == 0
  942.              || strncmp (cs->c_name,"LPB%",4) == 0
  943.              || strncmp (cs->c_name,"LBB%",4) == 0
  944.              || strncmp (cs->c_name,"LBE%",4) == 0
  945.              || strncmp (cs->c_name,"LPBX%",5) == 0))
  946.           /* At least on a 3b1, gcc generates swbeg and string labels
  947.          that look like this.  Ignore them.  */
  948.           break;
  949.         /* fall in for static symbols that don't start with '.' */
  950.       case C_EXT:
  951.         if (cs->c_sclass == C_EXT &&
  952.         cs->c_secnum == N_ABS &&
  953.         strcmp (cs->c_name, _ETEXT) == 0)
  954.             end_of_text_addr = cs->c_value;
  955.         if (!SDB_TYPE (cs->c_type)) {
  956.             if (cs->c_secnum <= 1) {    /* text or abs */
  957.                 record_misc_function (cs->c_name, cs->c_value);
  958.                 break;
  959.             } else {
  960.                 cs->c_type = T_INT;
  961.             }
  962.         }
  963.         (void) process_coff_symbol (cs, &main_aux);
  964.         break;
  965.  
  966.       case C_FCN:
  967.         if (strcmp (cs->c_name, ".bf") == 0)
  968.           {
  969.         within_function = 1;
  970.  
  971.         /* value contains address of first non-init type code */
  972.         /* main_aux.x_sym.x_misc.x_lnsz.x_lnno
  973.                 contains line number of '{' } */
  974.         fcn_first_line = main_aux.x_sym.x_misc.x_lnsz.x_lnno;
  975.  
  976.         new = (struct context_stack *)
  977.           xmalloc (sizeof (struct context_stack));
  978.         new->depth = depth = 0;
  979.         new->next = 0;
  980.         context_stack = new;
  981.         new->locals = 0;
  982.         new->old_blocks = pending_blocks;
  983.         new->start_addr = fcn_start_addr;
  984.         fcn_cs_saved.c_name = getsymname (&fcn_sym_saved);
  985.         new->name = process_coff_symbol (&fcn_cs_saved,
  986.                          &fcn_aux_saved);
  987.           }
  988.         else if (strcmp (cs->c_name, ".ef") == 0)
  989.           {
  990.               /* the value of .ef is the address of epilogue code;
  991.                * not useful for gdb
  992.                */
  993.         /* { main_aux.x_sym.x_misc.x_lnsz.x_lnno
  994.                 contains number of lines to '}' */
  995.         new = context_stack;
  996.         if (new == 0)
  997.           {
  998.             complain (&ef_complaint, cs->c_symnum);
  999.             within_function = 0;
  1000.             break;
  1001.           }
  1002.         fcn_last_line = main_aux.x_sym.x_misc.x_lnsz.x_lnno;
  1003.         enter_linenos (fcn_line_ptr, fcn_first_line, fcn_last_line);
  1004.  
  1005.         finish_block (new->name, &local_symbols, new->old_blocks,
  1006.                   new->start_addr,
  1007. #if defined (FUNCTION_EPILOGUE_SIZE)
  1008.                   /* This macro should be defined only on
  1009.                  machines where the
  1010.                  fcn_aux_saved.x_sym.x_misc.x_fsize
  1011.                  field is always zero.
  1012.                  So use the .bf record information that
  1013.                  points to the epilogue and add the size
  1014.                  of the epilogue.  */
  1015.                   cs->c_value + FUNCTION_EPILOGUE_SIZE
  1016. #else
  1017.                   fcn_cs_saved.c_value +
  1018.                       fcn_aux_saved.x_sym.x_misc.x_fsize
  1019. #endif
  1020.                   );
  1021.         context_stack = 0;
  1022.         within_function = 0;
  1023.         free (new);
  1024.           }
  1025.         break;
  1026.  
  1027.       case C_BLOCK:
  1028.         if (strcmp (cs->c_name, ".bb") == 0)
  1029.           {
  1030.         new = (struct context_stack *)
  1031.                 xmalloc (sizeof (struct context_stack));
  1032.         depth++;
  1033.         new->depth = depth;
  1034.         new->next = context_stack;
  1035.         context_stack = new;
  1036.         new->locals = local_symbols;
  1037.         new->old_blocks = pending_blocks;
  1038.         new->start_addr = cs->c_value;
  1039.         new->name = 0;
  1040.         local_symbols = 0;
  1041.           }
  1042.         else if (strcmp (cs->c_name, ".eb") == 0)
  1043.           {
  1044.         new = context_stack;
  1045.         if (new == 0 || depth != new->depth)
  1046.           error ("Invalid symbol data: .bb/.eb symbol mismatch at symbol %d.",
  1047.              symnum);
  1048.         if (local_symbols && context_stack->next)
  1049.           {
  1050.             /* Make a block for the local symbols within.  */
  1051.             finish_block (0, &local_symbols, new->old_blocks,
  1052.                   new->start_addr, cs->c_value);
  1053.           }
  1054.         depth--;
  1055.         local_symbols = new->locals;
  1056.         context_stack = new->next;
  1057.         free (new);
  1058.           }
  1059.         break;
  1060. #ifdef TDESC
  1061.           case C_VERSION:
  1062. #if 0
  1063.         /* This isn't used currently.  */
  1064.             if (strcmp (cs->c_name, ".coffsyn") == 0)
  1065.         last_coffsyn = cs->c_value;
  1066.         else
  1067. #endif /* 0 */
  1068.           if ((strcmp (cs->c_name, ".coffsem") == 0) &&
  1069.                      (cs->c_value != 0))
  1070.         last_coffsem = cs->c_value;
  1071.             break;
  1072. #endif /* TDESC */
  1073.  
  1074.       default:
  1075. #ifdef TDESC
  1076.         if ((strcmp (cs->c_name, ".coffsem") == 0) &&
  1077.         (cs->c_value != 0))
  1078.           last_coffsem = cs->c_value;
  1079.             else
  1080. #endif
  1081.         (void) process_coff_symbol (cs, &main_aux);
  1082.         break;
  1083.     }
  1084.     }
  1085.  
  1086.   if (last_source_file)
  1087.     end_symtab ();
  1088.   fclose (stream);
  1089.   discard_cleanups (old_chain);
  1090. }
  1091.  
  1092. /* Routines for reading headers and symbols from executable.  */
  1093.  
  1094. #ifdef FIXME
  1095. /* Move these XXXMAGIC symbol defns into BFD!  */
  1096.  
  1097. /* Read COFF file header, check magic number,
  1098.    and return number of symbols. */
  1099. read_file_hdr (chan, file_hdr)
  1100.     int chan;
  1101.     FILHDR *file_hdr;
  1102. {
  1103.   lseek (chan, 0L, 0);
  1104.   if (myread (chan, (char *)file_hdr, FILHSZ) < 0)
  1105.     return -1;
  1106.  
  1107.   switch (file_hdr->f_magic)
  1108.     {
  1109. #ifdef MC68MAGIC
  1110.     case MC68MAGIC:
  1111. #endif
  1112. #ifdef NS32GMAGIC
  1113.       case NS32GMAGIC:
  1114.       case NS32SMAGIC:
  1115. #endif
  1116. #ifdef I386MAGIC
  1117.     case I386MAGIC:
  1118. #endif
  1119. #ifdef CLIPPERMAGIC
  1120.     case CLIPPERMAGIC:
  1121. #endif
  1122. #if defined (MC68KWRMAGIC) \
  1123.   && (!defined (MC68MAGIC) || MC68KWRMAGIC != MC68MAGIC)
  1124.     case MC68KWRMAGIC:
  1125. #endif
  1126. #ifdef MC68KROMAGIC
  1127.     case MC68KROMAGIC:
  1128.     case MC68KPGMAGIC:
  1129. #endif
  1130. #ifdef MC88DGMAGIC
  1131.     case MC88DGMAGIC:
  1132. #endif      
  1133. #ifdef MC88MAGIC
  1134.     case MC88MAGIC:
  1135. #endif      
  1136. #ifdef I960ROMAGIC
  1137.     case I960ROMAGIC:        /* Intel 960 */
  1138. #endif
  1139. #ifdef I960RWMAGIC
  1140.     case I960RWMAGIC:        /* Intel 960 */
  1141. #endif
  1142.     return file_hdr->f_nsyms;
  1143.  
  1144.       default:
  1145. #ifdef BADMAG
  1146.     if (BADMAG(file_hdr))
  1147.       return -1;
  1148.     else
  1149.       return file_hdr->f_nsyms;
  1150. #else
  1151.     return -1;
  1152. #endif
  1153.     }
  1154. }
  1155. #endif
  1156.  
  1157. /* Read the next symbol, swap it, and return it in both internal_syment
  1158.    form, and coff_symbol form.  Also return its first auxent, if any,
  1159.    in internal_auxent form, and skip any other auxents.  */
  1160.  
  1161. static void
  1162. read_one_sym (cs, sym, aux)
  1163.     register struct coff_symbol *cs;
  1164.     register struct internal_syment *sym;
  1165.     register union internal_auxent *aux;
  1166. {
  1167.   struct external_syment temp_sym[1];
  1168.   union external_auxent temp_aux[1];
  1169.   int i;
  1170.  
  1171.   cs->c_symnum = symnum;
  1172.   fread ((char *)temp_sym, SYMESZ, 1, nlist_stream_global);
  1173.   bfd_coff_swap_sym_in (symfile_bfd, temp_sym, sym);
  1174.   cs->c_nsyms = (sym->n_numaux & 0xff) + 1;
  1175.   if (cs->c_nsyms >= 2)
  1176.     {
  1177.     fread ((char *)temp_aux, AUXESZ, 1, nlist_stream_global);
  1178.     bfd_coff_swap_aux_in (symfile_bfd, temp_aux, sym->n_type, sym->n_sclass, aux);
  1179.     /* If more than one aux entry, read past it (only the first aux
  1180.        is important). */
  1181.     for (i = 2; i < cs->c_nsyms; i++)
  1182.       fread ((char *)temp_aux, AUXESZ, 1, nlist_stream_global);
  1183.     }
  1184.   cs->c_name = getsymname (sym);
  1185.   cs->c_value = sym->n_value;
  1186.   cs->c_sclass = (sym->n_sclass & 0xff);
  1187.   cs->c_secnum = sym->n_scnum;
  1188.   cs->c_type = (unsigned) sym->n_type;
  1189.   if (!SDB_TYPE (cs->c_type))
  1190.     cs->c_type = 0;
  1191.  
  1192.   symnum += cs->c_nsyms;
  1193. }
  1194.  
  1195. /* Support for string table handling */
  1196.  
  1197. static char *stringtab = NULL;
  1198.  
  1199. static int
  1200. init_stringtab (chan, offset)
  1201.     int chan;
  1202.     long offset;
  1203. {
  1204.   long length;
  1205.   int val;
  1206.   unsigned char lengthbuf[4];
  1207.  
  1208.   if (stringtab)
  1209.     {
  1210.       free (stringtab);
  1211.       stringtab = NULL;
  1212.     }
  1213.  
  1214.   if (lseek (chan, offset, 0) < 0)
  1215.     return -1;
  1216.  
  1217.   val = myread (chan, (char *)lengthbuf, sizeof lengthbuf);
  1218.   length = bfd_h_get_32 (symfile_bfd, lengthbuf);
  1219.  
  1220.   /* If no string table is needed, then the file may end immediately
  1221.      after the symbols.  Just return with `stringtab' set to null. */
  1222.   if (val != sizeof length || length < sizeof length)
  1223.     return 0;
  1224.  
  1225.   stringtab = (char *) xmalloc (length);
  1226.   if (stringtab == NULL)
  1227.     return -1;
  1228.  
  1229.   bcopy (&length, stringtab, sizeof length);
  1230.   if (length == sizeof length)        /* Empty table -- just the count */
  1231.     return 0;
  1232.  
  1233.   val = myread (chan, stringtab + sizeof length, length - sizeof length);
  1234.   if (val != length - sizeof length || stringtab[length - 1] != '\0')
  1235.     return -1;
  1236.  
  1237.   return 0;
  1238. }
  1239.  
  1240. static void
  1241. free_stringtab ()
  1242. {
  1243.   if (stringtab)
  1244.     free (stringtab);
  1245.   stringtab = NULL;
  1246. }
  1247.  
  1248. static char *
  1249. getsymname (symbol_entry)
  1250.     struct internal_syment *symbol_entry;
  1251. {
  1252.   static char buffer[SYMNMLEN+1];
  1253.   char *result;
  1254.  
  1255.   if (symbol_entry->_n._n_n._n_zeroes == 0)
  1256.     {
  1257.       result = stringtab + symbol_entry->_n._n_n._n_offset;
  1258.     }
  1259.   else
  1260.     {
  1261.       strncpy (buffer, symbol_entry->_n._n_name, SYMNMLEN);
  1262.       buffer[SYMNMLEN] = '\0';
  1263.       result = buffer;
  1264.     }
  1265.   return result;
  1266. }
  1267.  
  1268. static char *
  1269. getfilename (aux_entry)
  1270.     union internal_auxent *aux_entry;
  1271. {
  1272.   static char buffer[BUFSIZ];
  1273.   register char *temp;
  1274.   char *result;
  1275.   extern char *rindex ();
  1276.  
  1277. #ifndef COFF_NO_LONG_FILE_NAMES
  1278. #if defined (x_zeroes)
  1279.   /* Data General.  */
  1280.   if (aux_entry->x_zeroes == 0)
  1281.     strcpy (buffer, stringtab + aux_entry->x_offset);
  1282. #else /* no x_zeroes */
  1283.   if (aux_entry->x_file.x_n.x_zeroes == 0)
  1284.     strcpy (buffer, stringtab + aux_entry->x_file.x_n.x_offset);
  1285. #endif /* no x_zeroes */
  1286.   else
  1287. #endif /* COFF_NO_LONG_FILE_NAMES */
  1288.     {
  1289. #if defined (x_name)
  1290.       /* Data General.  */
  1291.       strncpy (buffer, aux_entry->x_name, FILNMLEN);
  1292. #else
  1293.       strncpy (buffer, aux_entry->x_file.x_fname, FILNMLEN);
  1294. #endif
  1295.       buffer[FILNMLEN] = '\0';
  1296.     }
  1297.   result = buffer;
  1298.   if ((temp = rindex (result, '/')) != NULL)
  1299.     result = temp + 1;
  1300.   return (result);
  1301. }
  1302.  
  1303. /* Support for line number handling */
  1304. static char *linetab = NULL;
  1305. static long linetab_offset;
  1306. static unsigned long linetab_size;
  1307.  
  1308. /* Read in all the line numbers for fast lookups later.  Leave them in
  1309.    external (unswapped) format in memory; we'll swap them as we enter
  1310.    them into GDB's data structures.  */
  1311.  
  1312. static int
  1313. init_lineno (chan, offset, size)
  1314.     int chan;
  1315.     long offset;
  1316.     int size;
  1317. {
  1318.   int val;
  1319.  
  1320.   linetab_offset = offset;
  1321.   linetab_size = size;
  1322.  
  1323.   if (size == 0)
  1324.     return 0;
  1325.  
  1326.   if (lseek (chan, offset, 0) < 0)
  1327.     return -1;
  1328.   
  1329.   linetab = (char *) xmalloc (size);
  1330.  
  1331.   val = myread (chan, linetab, size);
  1332.   if (val != size)
  1333.     return -1;
  1334.  
  1335.   make_cleanup (free, linetab);        /* Be sure it gets de-allocated. */
  1336.   return 0;
  1337. }
  1338.  
  1339. #if !defined (L_LNNO32)
  1340. #define L_LNNO32(lp) ((lp)->l_lnno)
  1341. #endif
  1342.  
  1343. static void
  1344. enter_linenos (file_offset, first_line, last_line)
  1345.     long file_offset;
  1346.     register int first_line;
  1347.     register int last_line;
  1348. {
  1349.   register char *rawptr;
  1350.   struct internal_lineno lptr;
  1351.  
  1352.   if (file_offset < linetab_offset)
  1353.     {
  1354.       complain (&lineno_complaint, file_offset);
  1355.       if (file_offset > linetab_size)    /* Too big to be an offset? */
  1356.     return;
  1357.       file_offset += linetab_offset;  /* Try reading at that linetab offset */
  1358.     }
  1359.   
  1360.   rawptr = &linetab[file_offset - linetab_offset];
  1361.  
  1362.   /* skip first line entry for each function */
  1363.   rawptr += LINESZ;
  1364.   /* line numbers start at one for the first line of the function */
  1365.   first_line--;
  1366.  
  1367.   for (;;) {
  1368.     bfd_coff_swap_lineno_in (symfile_bfd, (LINENO *)rawptr, &lptr);
  1369.     rawptr += LINESZ;
  1370.     if (L_LNNO32 (&lptr) && L_LNNO32 (&lptr) <= last_line)
  1371.       record_line (first_line + L_LNNO32 (&lptr), lptr.l_addr.l_paddr);
  1372.     else
  1373.       break;
  1374.   } 
  1375. }
  1376.  
  1377. static int
  1378. hashname (name)
  1379.      char *name;
  1380. {
  1381.   register char *p = name;
  1382.   register int total = p[0];
  1383.   register int c;
  1384.  
  1385.   c = p[1];
  1386.   total += c << 2;
  1387.   if (c)
  1388.     {
  1389.       c = p[2];
  1390.       total += c << 4;
  1391.       if (c)
  1392.     total += p[3] << 6;
  1393.     }
  1394.   
  1395.   return total % HASHSIZE;
  1396. }
  1397.  
  1398. static void
  1399. patch_type (type, real_type)
  1400.     struct type *type;
  1401.     struct type *real_type;
  1402. {
  1403.   register struct type *target = TYPE_TARGET_TYPE (type);
  1404.   register struct type *real_target = TYPE_TARGET_TYPE (real_type);
  1405.   int field_size = TYPE_NFIELDS (real_target) * sizeof (struct field);
  1406.  
  1407.   TYPE_LENGTH (target) = TYPE_LENGTH (real_target);
  1408.   TYPE_NFIELDS (target) = TYPE_NFIELDS (real_target);
  1409.   TYPE_FIELDS (target) = (struct field *)
  1410.                 obstack_alloc (symbol_obstack, field_size);
  1411.  
  1412.   bcopy (TYPE_FIELDS (real_target), TYPE_FIELDS (target), field_size);
  1413.  
  1414.   if (TYPE_NAME (real_target))
  1415.     {
  1416.       if (TYPE_NAME (target))
  1417.     free (TYPE_NAME (target));
  1418.       TYPE_NAME (target) = concat (TYPE_NAME (real_target), "", "");
  1419.     }
  1420. }
  1421.  
  1422. /* Patch up all appropriate typdef symbols in the opaque_type_chains
  1423.    so that they can be used to print out opaque data structures properly */
  1424.  
  1425. static void
  1426. patch_opaque_types ()
  1427. {
  1428.   struct symtab *s;
  1429.  
  1430.   /* Look at each symbol in the per-file block of each symtab.  */
  1431.   for (s = symtab_list; s; s = s->next)
  1432.     {
  1433.       register struct block *b;
  1434.       register int i;
  1435.  
  1436.       /* Go through the per-file symbols only */
  1437.       b = BLOCKVECTOR_BLOCK (BLOCKVECTOR (s), STATIC_BLOCK);
  1438.       for (i = BLOCK_NSYMS (b) - 1; i >= 0; i--)
  1439.     {
  1440.       register struct symbol *real_sym;
  1441.  
  1442.       /* Find completed typedefs to use to fix opaque ones.
  1443.          Remove syms from the chain when their types are stored,
  1444.          but search the whole chain, as there may be several syms
  1445.          from different files with the same name.  */
  1446.       real_sym = BLOCK_SYM (b, i);
  1447.       if (SYMBOL_CLASS (real_sym) == LOC_TYPEDEF &&
  1448.           SYMBOL_NAMESPACE (real_sym) == VAR_NAMESPACE &&
  1449.           TYPE_CODE (SYMBOL_TYPE (real_sym)) == TYPE_CODE_PTR &&
  1450.           TYPE_LENGTH (TYPE_TARGET_TYPE (SYMBOL_TYPE (real_sym))) != 0)
  1451.         {
  1452.           register char *name = SYMBOL_NAME (real_sym);
  1453.           register int hash = hashname (name);
  1454.           register struct symbol *sym, *prev;
  1455.  
  1456.           prev = 0;
  1457.           for (sym = opaque_type_chain[hash]; sym;)
  1458.         {
  1459.           if (name[0] == SYMBOL_NAME (sym)[0] &&
  1460.               !strcmp (name + 1, SYMBOL_NAME (sym) + 1))
  1461.             {
  1462.               if (prev)
  1463.             SYMBOL_VALUE_CHAIN (prev) = SYMBOL_VALUE_CHAIN (sym);
  1464.               else
  1465.             opaque_type_chain[hash] = SYMBOL_VALUE_CHAIN (sym);
  1466.  
  1467.               patch_type (SYMBOL_TYPE (sym), SYMBOL_TYPE (real_sym));
  1468.  
  1469.               if (prev)
  1470.             sym = SYMBOL_VALUE_CHAIN (prev);
  1471.               else
  1472.             sym = opaque_type_chain[hash];
  1473.             }
  1474.           else
  1475.             {
  1476.               prev = sym;
  1477.               sym = SYMBOL_VALUE_CHAIN (sym);
  1478.             }
  1479.         }
  1480.         }
  1481.     }
  1482.     }
  1483. }
  1484.  
  1485. #if defined (clipper)
  1486. #define BELIEVE_PCC_PROMOTION 1
  1487. #endif
  1488.  
  1489. static struct symbol *
  1490. process_coff_symbol (cs, aux)
  1491.      register struct coff_symbol *cs;
  1492.      register union internal_auxent *aux;
  1493. {
  1494.   register struct symbol *sym
  1495.     = (struct symbol *) obstack_alloc (symbol_obstack, sizeof (struct symbol));
  1496.   char *name;
  1497. #ifdef NAMES_HAVE_UNDERSCORE
  1498.   int offset = 1;
  1499. #else
  1500.   int offset = 0;
  1501. #endif
  1502.  
  1503.   bzero (sym, sizeof (struct symbol));
  1504.   name = cs->c_name;
  1505.   name = (name[0] == '_' ? name + offset : name);
  1506.   SYMBOL_NAME (sym) = obstack_copy0 (symbol_obstack, name, strlen (name));
  1507.  
  1508.   /* default assumptions */
  1509.   SYMBOL_VALUE (sym) = cs->c_value;
  1510.   SYMBOL_NAMESPACE (sym) = VAR_NAMESPACE;
  1511.  
  1512.   if (ISFCN (cs->c_type))
  1513.     {
  1514.       SYMBOL_TYPE (sym) = 
  1515.     lookup_function_type (decode_function_type (cs, cs->c_type, aux));
  1516.       SYMBOL_CLASS (sym) = LOC_BLOCK;
  1517.       if (cs->c_sclass == C_STAT)
  1518.     add_symbol_to_list (sym, &file_symbols);
  1519.       else if (cs->c_sclass == C_EXT)
  1520.     add_symbol_to_list (sym, &global_symbols);
  1521.     }
  1522.   else
  1523.     {
  1524.       SYMBOL_TYPE (sym) = decode_type (cs, cs->c_type, aux);
  1525.       switch (cs->c_sclass)
  1526.     {
  1527.       case C_NULL:
  1528.         break;
  1529.  
  1530.       case C_AUTO:
  1531.         SYMBOL_CLASS (sym) = LOC_LOCAL;
  1532.         add_symbol_to_list (sym, &local_symbols);
  1533.         break;
  1534.  
  1535.       case C_EXT:
  1536.         SYMBOL_CLASS (sym) = LOC_STATIC;
  1537.         SYMBOL_VALUE_ADDRESS (sym) = (CORE_ADDR) cs->c_value;
  1538.         add_symbol_to_list (sym, &global_symbols);
  1539.         break;
  1540.  
  1541.       case C_STAT:
  1542.         SYMBOL_CLASS (sym) = LOC_STATIC;
  1543.         SYMBOL_VALUE_ADDRESS (sym) = (CORE_ADDR) cs->c_value;
  1544.         if (within_function) {
  1545.           /* Static symbol of local scope */
  1546.           add_symbol_to_list (sym, &local_symbols);
  1547.         }
  1548.         else {
  1549.           /* Static symbol at top level of file */
  1550.           add_symbol_to_list (sym, &file_symbols);
  1551.         }
  1552.         break;
  1553.  
  1554.       case C_REG:
  1555.         SYMBOL_CLASS (sym) = LOC_REGISTER;
  1556.         add_symbol_to_list (sym, &local_symbols);
  1557.         break;
  1558.  
  1559.       case C_LABEL:
  1560.         break;
  1561.  
  1562.       case C_ARG:
  1563.         SYMBOL_CLASS (sym) = LOC_ARG;
  1564.         add_symbol_to_list (sym, &local_symbols);
  1565. #if !defined (BELIEVE_PCC_PROMOTION)
  1566.         /* If PCC says a parameter is a short or a char,
  1567.            it is really an int.  */
  1568.         if (SYMBOL_TYPE (sym) == builtin_type_char
  1569.         || SYMBOL_TYPE (sym) == builtin_type_short)
  1570.           SYMBOL_TYPE (sym) = builtin_type_int;
  1571.         else if (SYMBOL_TYPE (sym) == builtin_type_unsigned_char
  1572.              || SYMBOL_TYPE (sym) == builtin_type_unsigned_short)
  1573.           SYMBOL_TYPE (sym) = builtin_type_unsigned_int;
  1574. #endif
  1575.         break;
  1576.  
  1577.       case C_REGPARM:
  1578.         SYMBOL_CLASS (sym) = LOC_REGPARM;
  1579.         add_symbol_to_list (sym, &local_symbols);
  1580. #if !defined (BELIEVE_PCC_PROMOTION)
  1581.         /* If PCC says a parameter is a short or a char,
  1582.            it is really an int.  */
  1583.         if (SYMBOL_TYPE (sym) == builtin_type_char
  1584.         || SYMBOL_TYPE (sym) == builtin_type_short)
  1585.           SYMBOL_TYPE (sym) = builtin_type_int;
  1586.         else if (SYMBOL_TYPE (sym) == builtin_type_unsigned_char
  1587.              || SYMBOL_TYPE (sym) == builtin_type_unsigned_short)
  1588.           SYMBOL_TYPE (sym) = builtin_type_unsigned_int;
  1589. #endif
  1590.         break;
  1591.         
  1592.       case C_TPDEF:
  1593.         SYMBOL_CLASS (sym) = LOC_TYPEDEF;
  1594.         SYMBOL_NAMESPACE (sym) = VAR_NAMESPACE;
  1595.  
  1596.         /* If type has no name, give it one */
  1597.         if (TYPE_NAME (SYMBOL_TYPE (sym)) == 0 
  1598.         && (TYPE_FLAGS (SYMBOL_TYPE (sym)) & TYPE_FLAG_PERM) == 0)
  1599.           TYPE_NAME (SYMBOL_TYPE (sym))
  1600.                       = concat (SYMBOL_NAME (sym), "", "");
  1601.  
  1602.         /* Keep track of any type which points to empty structured type,
  1603.         so it can be filled from a definition from another file */
  1604.         if (TYPE_CODE (SYMBOL_TYPE (sym)) == TYPE_CODE_PTR &&
  1605.         TYPE_LENGTH (TYPE_TARGET_TYPE (SYMBOL_TYPE (sym))) == 0)
  1606.           {
  1607.         register int i = hashname (SYMBOL_NAME (sym));
  1608.  
  1609.         SYMBOL_VALUE_CHAIN (sym) = opaque_type_chain[i];
  1610.         opaque_type_chain[i] = sym;
  1611.           }
  1612.         add_symbol_to_list (sym, &file_symbols);
  1613.         break;
  1614.  
  1615.       case C_STRTAG:
  1616.       case C_UNTAG:
  1617.       case C_ENTAG:
  1618.         SYMBOL_CLASS (sym) = LOC_TYPEDEF;
  1619.         SYMBOL_NAMESPACE (sym) = STRUCT_NAMESPACE;
  1620.         if (TYPE_NAME (SYMBOL_TYPE (sym)) == 0
  1621.         && (TYPE_FLAGS (SYMBOL_TYPE (sym)) & TYPE_FLAG_PERM) == 0)
  1622.           TYPE_NAME (SYMBOL_TYPE (sym))
  1623.         = concat ("",
  1624.               (cs->c_sclass == C_ENTAG
  1625.                ? "enum "
  1626.                : (cs->c_sclass == C_STRTAG
  1627.                   ? "struct " : "union ")),
  1628.               SYMBOL_NAME (sym));
  1629.         add_symbol_to_list (sym, &file_symbols);
  1630.         break;
  1631.  
  1632.       default:
  1633.         break;
  1634.     }
  1635.     }
  1636.   return sym;
  1637. }
  1638.  
  1639. /* Decode a coff type specifier;
  1640.    return the type that is meant.  */
  1641.  
  1642. static
  1643. struct type *
  1644. decode_type (cs, c_type, aux)
  1645.      register struct coff_symbol *cs;
  1646.      unsigned int c_type;
  1647.      register union internal_auxent *aux;
  1648. {
  1649.   register struct type *type = 0;
  1650.   unsigned int new_c_type;
  1651.  
  1652.   if (c_type & ~N_BTMASK)
  1653.     {
  1654.       new_c_type = DECREF (c_type);
  1655.       if (ISPTR (c_type))
  1656.     {
  1657.       type = decode_type (cs, new_c_type, aux);
  1658.       type = lookup_pointer_type (type);
  1659.     }
  1660.       else if (ISFCN (c_type))
  1661.     {
  1662.       type = decode_type (cs, new_c_type, aux);
  1663.       type = lookup_function_type (type);
  1664.     }
  1665.       else if (ISARY (c_type))
  1666.     {
  1667.       int i, n;
  1668.       register unsigned short *dim;
  1669.       struct type *base_type;
  1670.  
  1671.       /* Define an array type.  */
  1672.       /* auxent refers to array, not base type */
  1673.       if (aux->x_sym.x_tagndx.l == 0)
  1674.         cs->c_nsyms = 1;
  1675.  
  1676.       /* shift the indices down */
  1677.       dim = &aux->x_sym.x_fcnary.x_ary.x_dimen[0];
  1678.       i = 1;
  1679.       n = dim[0];
  1680.       for (i = 0; *dim && i < DIMNUM - 1; i++, dim++)
  1681.         *dim = *(dim + 1);
  1682.       *dim = 0;
  1683.  
  1684.       type = (struct type *)
  1685.             obstack_alloc (symbol_obstack, sizeof (struct type));
  1686.       bzero (type, sizeof (struct type));
  1687.  
  1688.       base_type = decode_type (cs, new_c_type, aux);
  1689.  
  1690.       TYPE_CODE (type) = TYPE_CODE_ARRAY;
  1691.       TYPE_TARGET_TYPE (type) = base_type;
  1692.       TYPE_LENGTH (type) = n * TYPE_LENGTH (base_type);
  1693.     }
  1694.       return type;
  1695.     }
  1696.  
  1697.   /* Reference to existing type */
  1698.   if (cs->c_nsyms > 1 && aux->x_sym.x_tagndx.l != 0)
  1699.     {
  1700.       type = coff_alloc_type (aux->x_sym.x_tagndx.l);
  1701.       return type;
  1702.     }
  1703.  
  1704.   return decode_base_type (cs, BTYPE (c_type), aux);
  1705. }
  1706.  
  1707. /* Decode a coff type specifier for function definition;
  1708.    return the type that the function returns.  */
  1709.  
  1710. static
  1711. struct type *
  1712. decode_function_type (cs, c_type, aux)
  1713.      register struct coff_symbol *cs;
  1714.      unsigned int c_type;
  1715.      register union internal_auxent *aux;
  1716. {
  1717.   if (aux->x_sym.x_tagndx.l == 0)
  1718.     cs->c_nsyms = 1;    /* auxent refers to function, not base type */
  1719.  
  1720.   return decode_type (cs, DECREF (c_type), aux);
  1721. }
  1722.  
  1723. /* basic C types */
  1724.  
  1725. static
  1726. struct type *
  1727. decode_base_type (cs, c_type, aux)
  1728.      register struct coff_symbol *cs;
  1729.      unsigned int c_type;
  1730.      register union internal_auxent *aux;
  1731. {
  1732.   struct type *type;
  1733.  
  1734.   switch (c_type)
  1735.     {
  1736.       case T_NULL:
  1737.         /* shows up with "void (*foo)();" structure members */
  1738.     return builtin_type_void;
  1739.  
  1740. #if 0
  1741. /* DGUX actually defines both T_ARG and T_VOID to the same value.  */
  1742. #ifdef T_ARG
  1743.       case T_ARG:
  1744.     /* Shows up in DGUX, I think.  Not sure where.  */
  1745.     return builtin_type_void;    /* shouldn't show up here */
  1746. #endif
  1747. #endif /* 0 */
  1748.  
  1749. #ifdef T_VOID
  1750.       case T_VOID:
  1751.     /* Intel 960 COFF has this symbol and meaning.  */
  1752.     return builtin_type_void;
  1753. #endif
  1754.  
  1755.       case T_CHAR:
  1756.     return builtin_type_char;
  1757.  
  1758.       case T_SHORT:
  1759.     return builtin_type_short;
  1760.  
  1761.       case T_INT:
  1762.     return builtin_type_int;
  1763.  
  1764.       case T_LONG:
  1765.     return builtin_type_long;
  1766.  
  1767.       case T_FLOAT:
  1768.     return builtin_type_float;
  1769.  
  1770.       case T_DOUBLE:
  1771.     return builtin_type_double;
  1772.  
  1773.       case T_STRUCT:
  1774.     if (cs->c_nsyms != 2)
  1775.       {
  1776.         /* anonymous structure type */
  1777.         type = coff_alloc_type (cs->c_symnum);
  1778.         TYPE_CODE (type) = TYPE_CODE_STRUCT;
  1779.         TYPE_NAME (type) = concat ("struct ", "<opaque>", "");
  1780.         TYPE_LENGTH (type) = 0;
  1781.         TYPE_FIELDS (type) = 0;
  1782.         TYPE_NFIELDS (type) = 0;
  1783.       }
  1784.     else
  1785.       {
  1786.         type = read_struct_type (cs->c_symnum,
  1787.                     aux->x_sym.x_misc.x_lnsz.x_size,
  1788.                     aux->x_sym.x_fcnary.x_fcn.x_endndx);
  1789.       }
  1790.     return type;
  1791.  
  1792.       case T_UNION:
  1793.     if (cs->c_nsyms != 2)
  1794.       {
  1795.         /* anonymous union type */
  1796.         type = coff_alloc_type (cs->c_symnum);
  1797.         TYPE_NAME (type) = concat ("union ", "<opaque>", "");
  1798.         TYPE_LENGTH (type) = 0;
  1799.         TYPE_FIELDS (type) = 0;
  1800.         TYPE_NFIELDS (type) = 0;
  1801.       }
  1802.     else
  1803.       {
  1804.         type = read_struct_type (cs->c_symnum,
  1805.                     aux->x_sym.x_misc.x_lnsz.x_size,
  1806.                     aux->x_sym.x_fcnary.x_fcn.x_endndx);
  1807.       }
  1808.     TYPE_CODE (type) = TYPE_CODE_UNION;
  1809.     return type;
  1810.  
  1811.       case T_ENUM:
  1812.     return read_enum_type (cs->c_symnum,
  1813.                     aux->x_sym.x_misc.x_lnsz.x_size,
  1814.                     aux->x_sym.x_fcnary.x_fcn.x_endndx);
  1815.  
  1816.       case T_MOE:
  1817.     /* shouldn't show up here */
  1818.     break;
  1819.  
  1820.       case T_UCHAR:
  1821.     return builtin_type_unsigned_char;
  1822.  
  1823.       case T_USHORT:
  1824.     return builtin_type_unsigned_short;
  1825.  
  1826.       case T_UINT:
  1827.     return builtin_type_unsigned_int;
  1828.  
  1829.       case T_ULONG:
  1830.     return builtin_type_unsigned_long;
  1831.     }
  1832.   printf ("unexpected type %d at symnum %d\n", c_type, cs->c_symnum);
  1833.   return builtin_type_void;
  1834. }
  1835.  
  1836. /* This page contains subroutines of read_type.  */
  1837.  
  1838. /* Read the description of a structure (or union type)
  1839.    and return an object describing the type.  */
  1840.  
  1841. static struct type *
  1842. read_struct_type (index, length, lastsym)
  1843.      int index;
  1844.      int length;
  1845.      int lastsym;
  1846. {
  1847.   struct nextfield
  1848.     {
  1849.       struct nextfield *next;
  1850.       struct field field;
  1851.     };
  1852.  
  1853.   register struct type *type;
  1854.   register struct nextfield *list = 0;
  1855.   struct nextfield *new;
  1856.   int nfields = 0;
  1857.   register int n;
  1858.   char *name;
  1859. #ifdef NAMES_HAVE_UNDERSCORE
  1860.   int offset = 1;
  1861. #else
  1862.   int offset = 0;
  1863. #endif
  1864.   struct coff_symbol member_sym;
  1865.   register struct coff_symbol *ms = &member_sym;
  1866.   struct internal_syment sub_sym;
  1867.   union internal_auxent sub_aux;
  1868.   int done = 0;
  1869.  
  1870.   type = coff_alloc_type (index);
  1871.   TYPE_CODE (type) = TYPE_CODE_STRUCT;
  1872.   TYPE_LENGTH (type) = length;
  1873.  
  1874.   while (!done && symnum < lastsym && symnum < nlist_nsyms_global)
  1875.     {
  1876.       read_one_sym (ms, &sub_sym, &sub_aux);
  1877.       name = ms->c_name;
  1878.       name = (name[0] == '_' ? name + offset : name);
  1879.  
  1880.       switch (ms->c_sclass)
  1881.     {
  1882.       case C_MOS:
  1883.       case C_MOU:
  1884.  
  1885.         /* Get space to record the next field's data.  */
  1886.         new = (struct nextfield *) alloca (sizeof (struct nextfield));
  1887.         new->next = list;
  1888.         list = new;
  1889.  
  1890.         /* Save the data.  */
  1891.         list->field.name = savestring (name, strlen (name));
  1892.         list->field.type = decode_type (ms, ms->c_type, &sub_aux);
  1893.         list->field.bitpos = 8 * ms->c_value;
  1894.         list->field.bitsize = 0;
  1895.         nfields++;
  1896.         break;
  1897.  
  1898.       case C_FIELD:
  1899.  
  1900.         /* Get space to record the next field's data.  */
  1901.         new = (struct nextfield *) alloca (sizeof (struct nextfield));
  1902.         new->next = list;
  1903.         list = new;
  1904.  
  1905.         /* Save the data.  */
  1906.         list->field.name = savestring (name, strlen (name));
  1907.         list->field.type = decode_type (ms, ms->c_type, &sub_aux);
  1908.         list->field.bitpos = ms->c_value;
  1909.         list->field.bitsize = sub_aux.x_sym.x_misc.x_lnsz.x_size;
  1910.         nfields++;
  1911.         break;
  1912.  
  1913.       case C_EOS:
  1914.         done = 1;
  1915.         break;
  1916.     }
  1917.     }
  1918.   /* Now create the vector of fields, and record how big it is.  */
  1919.  
  1920.   TYPE_NFIELDS (type) = nfields;
  1921.   TYPE_FIELDS (type) = (struct field *)
  1922.         obstack_alloc (symbol_obstack, sizeof (struct field) * nfields);
  1923.  
  1924.   /* Copy the saved-up fields into the field vector.  */
  1925.  
  1926.   for (n = nfields; list; list = list->next)
  1927.     TYPE_FIELD (type, --n) = list->field;
  1928.  
  1929.   return type;
  1930. }
  1931.  
  1932. /* Read a definition of an enumeration type,
  1933.    and create and return a suitable type object.
  1934.    Also defines the symbols that represent the values of the type.  */
  1935. /* Currently assumes it's sizeof (int) and doesn't use length.  */
  1936.  
  1937. /* ARGSUSED */
  1938. static struct type *
  1939. read_enum_type (index, length, lastsym)
  1940.      int index;
  1941.      int length;
  1942.      int lastsym;
  1943. {
  1944.   register struct symbol *sym;
  1945.   register struct type *type;
  1946.   int nsyms = 0;
  1947.   int done = 0;
  1948.   struct pending **symlist;
  1949.   struct coff_symbol member_sym;
  1950.   register struct coff_symbol *ms = &member_sym;
  1951.   struct internal_syment sub_sym;
  1952.   union internal_auxent sub_aux;
  1953.   struct pending *osyms, *syms;
  1954.   register int n;
  1955.   char *name;
  1956. #ifdef NAMES_HAVE_UNDERSCORE
  1957.   int offset = 1;
  1958. #else
  1959.   int offset = 0;
  1960. #endif
  1961.  
  1962.   type = coff_alloc_type (index);
  1963.   if (within_function)
  1964.     symlist = &local_symbols;
  1965.   else
  1966.     symlist = &file_symbols;
  1967.   osyms = *symlist;
  1968.  
  1969.   while (!done && symnum < lastsym && symnum < nlist_nsyms_global)
  1970.     {
  1971.       read_one_sym (ms, &sub_sym, &sub_aux);
  1972.       name = ms->c_name;
  1973.       name = (name[0] == '_' ? name + offset : name);
  1974.  
  1975.       switch (ms->c_sclass)
  1976.     {
  1977.       case C_MOE:
  1978.         sym = (struct symbol *) xmalloc (sizeof (struct symbol));
  1979.         bzero (sym, sizeof (struct symbol));
  1980.  
  1981.         SYMBOL_NAME (sym) = savestring (name, strlen (name));
  1982.         SYMBOL_CLASS (sym) = LOC_CONST;
  1983.         SYMBOL_NAMESPACE (sym) = VAR_NAMESPACE;
  1984.         SYMBOL_VALUE (sym) = ms->c_value;
  1985.         add_symbol_to_list (sym, symlist);
  1986.         nsyms++;
  1987.         break;
  1988.  
  1989.       case C_EOS:
  1990.         /* Sometimes the linker (on 386/ix 2.0.2 at least) screws
  1991.            up the count of how many symbols to read.  So stop
  1992.            on .eos.  */
  1993.         done = 1;
  1994.         break;
  1995.     }
  1996.     }
  1997.  
  1998.   /* Now fill in the fields of the type-structure.  */
  1999.  
  2000.   /* FIXME: Should be sizeof (int) on target, not host.  */
  2001.   TYPE_LENGTH (type) = sizeof (int);
  2002.   TYPE_CODE (type) = TYPE_CODE_ENUM;
  2003.   TYPE_NFIELDS (type) = nsyms;
  2004.   TYPE_FIELDS (type) = (struct field *)
  2005.         obstack_alloc (symbol_obstack, sizeof (struct field) * nsyms);
  2006.  
  2007.   /* Find the symbols for the values and put them into the type.
  2008.      The symbols can be found in the symlist that we put them on
  2009.      to cause them to be defined.  osyms contains the old value
  2010.      of that symlist; everything up to there was defined by us.  */
  2011.  
  2012.   for (syms = *symlist, n = nsyms; syms != osyms; syms = syms->next)
  2013.     {
  2014.       SYMBOL_TYPE (syms->symbol) = type;
  2015.       TYPE_FIELD_NAME (type, --n) = SYMBOL_NAME (syms->symbol);
  2016.       TYPE_FIELD_VALUE (type, n) = 0;
  2017.       TYPE_FIELD_BITPOS (type, n) = SYMBOL_VALUE (syms->symbol);
  2018.       TYPE_FIELD_BITSIZE (type, n) = 0;
  2019.     }
  2020.   return type;
  2021. }
  2022.  
  2023. /* Register our ability to parse symbols for coff BFD files */
  2024.  
  2025. static struct sym_fns coff_sym_fns =
  2026. {
  2027.     /* This assumes that 88kbcs implies TDESC and TDESC implies 88kbcs.
  2028.        If that's not true, this can be relaxed, but if it is true,
  2029.        it will just cause users grief if we try to read the wrong kind
  2030.        of symbol file.  */
  2031. #if defined (TDESC)
  2032.     "m88kbcs", 8,
  2033. #else /* not TDESC */
  2034.     "coff", 4,
  2035. #endif /* not TDESC */
  2036.     coff_new_init, coff_symfile_init, coff_symfile_read,
  2037. };
  2038.  
  2039. void
  2040. _initialize_coffread ()
  2041. {
  2042.   add_symtab_fns(&coff_sym_fns);
  2043. }
  2044.